﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using com.dz.ht.components;
using System.Diagnostics;
using com.dz.ht.common;

namespace com.dz.ht.ui
{
    public partial class FrmMain : Form
    {


        public FrmMain()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        /// <summary>
        /// 书籍
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 书籍ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BookSite t_book=new BookSite();
            t_book.SendMsg += new SendMsgEventHandler(DisplayMsg);
            Thread thd = new Thread(t_book.GetAiYunBookList);
          
            thd.Start();
            /*
            FrmBook frmBook = new FrmBook();
            DialogResult dlr = frmBook.ShowDialog();
            if (dlr == DialogResult.OK)
            {
                string t_site_id_list = frmBook.SiteIdList;
                string[] t_site_arr = t_site_id_list.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);


                for (int index = 0; index < t_site_arr.Length; index++)
                {
                    NetBookManger t_net_mgr = new NetBookManger(t_site_arr[index]);
                    t_net_mgr.InDB = frmBook.InDB;
                    t_net_mgr.SiteName = t_site_arr[index];
                    t_net_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);

                    //具体地址获取
                    if (!string.IsNullOrEmpty(frmBook.Url) && frmBook.CollectTypeList.IndexOf("0,") > -1)
                    {
                        Thread thd = new Thread(t_net_mgr.StartBookSignUrl);
                        t_net_mgr.Url = frmBook.Url;
                        thd.Name = t_site_arr[index];
                        thd.Start();
                       
                    }
                    //更新列表
                    if (frmBook.CollectTypeList.IndexOf("1,") > -1)
                    {

                        Thread thd = new Thread(t_net_mgr.StartListBook);
                        thd.Name = t_site_arr[index];
                        thd.Start();
                    }
                    //最近更新
                    if (frmBook.CollectTypeList.IndexOf("2,") > -1)
                    {
                        Thread thd = new Thread(t_net_mgr.StartListBook);
                        thd.Name = t_site_arr[index];
                        thd.Start();
                    }

                    if (frmBook.CollectTypeList.IndexOf("4,") > -1)
                    {
                        //Thread thd = new Thread(t_net_mgr.StartDefaultBook);
                        //thd.Name = "default";
                        //thd.Start();

                    }

                  

                }
            }*/

        }

        /// <summary>
        /// 显示处理回送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DisplayMsg(object sender, SendMsgEventArgs e)
        {
            txtInfo.AppendText(e.Msg);
            txtInfo.AppendText("\r\n");
            txtInfo.AppendText("\r\n");

        }

        private void 封面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmServices frm = new FrmServices();
            frm.ShowDialog();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            KillProcess("HT-1.exe");
            Application.ExitThread();
        }

        #region 杀死进程
        private void KillProcess(string processName)
        {
            //获得进程对象，以用来操作   
            System.Diagnostics.Process myproc = new System.Diagnostics.Process();
            //得到所有打开的进程    
            try
            {
                //获得需要杀死的进程名   
                foreach (Process thisproc in Process.GetProcessesByName(processName))
                {
                    //立即杀死进程   
                    thisproc.Kill();
                }
            }
            catch (Exception Exc)
            {
                throw new Exception("", Exc);
            }
        }
        #endregion

        private void 评论ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CollectBookReviewManager t_review_mgr = new CollectBookReviewManager();

            Thread thd = new Thread(t_review_mgr.StartCollectReview);
            t_review_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);

            thd.Name = "review";
            thd.Start();
        }

        /// <summary>
        /// 搜索封面及评论
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 封面及评论ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            BaiDuICONManager t_baidu = new BaiDuICONManager();

            t_baidu.SendMsg += new SendMsgEventHandler(DisplayMsg);
            Thread thd = new Thread(t_baidu.StartImageFromQD);
            thd.Start();
           
            /*
            FrmReview t_frm_review = new FrmReview();
            DialogResult t_dlr = t_frm_review.ShowDialog();
            Thread thd = null;
            if (t_dlr == DialogResult.OK)
            {
                if (t_frm_review.ComList.IndexOf("17k") > -1)
                {
                    ICON_ReviewManager t_icon_mgr = new ICON_ReviewManager();
                    t_icon_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
                    thd = new Thread(t_icon_mgr.StartAllBook);

                }
                if (t_frm_review.ComList.IndexOf("贴吧") > -1)
                {
                    CollectBookReviewManager t_review_mgr = new CollectBookReviewManager();
                    t_review_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
                    thd = new Thread(t_review_mgr.StartCollectReview);

                }

                if (t_frm_review.ComList.IndexOf("百度") > -1)
                {
                    BaiDuICONManager t_baidu = new BaiDuICONManager();
                    t_baidu.StartBookId = t_frm_review.StartBookId;
                    t_baidu.IsICON = true;
                    t_baidu.SendMsg += new SendMsgEventHandler(DisplayMsg);
                    thd = new Thread(t_baidu.StartCollect);
                }
            }
            if (thd != null)
            {
                thd.Name = "review";
                thd.Start();
            }*/
           
        }

       

        private void 评论ToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            BaiDuICONManager t_review_mgr = new BaiDuICONManager();
           
            t_review_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
            Thread thd = null;
            thd = new Thread(t_review_mgr.StartReviewFromQD);

            thd.Start();

            /*
            //string t_html = com.dz.ht.common.NetSiteCatchManager.ReadUrl("http://www.laishu.com/book/5/5272/3651569.shtml", Encoding.GetEncoding("gb2312"));

            FrmReview t_frm_review = new FrmReview();
            DialogResult t_dlr = t_frm_review.ShowDialog();
            Thread thd = null;
            if (t_dlr == DialogResult.OK)
            {
                if (t_frm_review.ComList.IndexOf("17k") > -1)
                {
                    ICON_ReviewManager t_icon_mgr = new ICON_ReviewManager();
                    t_icon_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
                    thd = new Thread(t_icon_mgr.StartAllBook);

                }
                if (t_frm_review.ComList.IndexOf("贴吧") > -1)
                {
                    CollectBookReviewManager t_review_mgr = new CollectBookReviewManager();
                    t_review_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
                    thd = new Thread(t_review_mgr.StartCollectReview);

                }

                if (t_frm_review.ComList.IndexOf("百度") > -1)
                {
                    BaiDuICONManager t_baidu = new BaiDuICONManager();
                    t_baidu.StartBookId = t_frm_review.StartBookId;
                    
                    t_baidu.SendMsg += new SendMsgEventHandler(DisplayMsg);
                    thd = new Thread(t_baidu.StartCollect);
                }
            }
            if (thd != null)
            {
                thd.Name = "review";
                thd.Start();
            }*/
        }

        private void 书籍列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmBookSearchSite t_frm = new FrmBookSearchSite();
            t_frm.ShowDialog();
        }

        private void vIP书籍ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VIPBookCollectManager t_vip_mgr = new VIPBookCollectManager();
            t_vip_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
            Thread thd = new Thread(t_vip_mgr.StartVIPCollect);
            thd.Start();
        }

        private void 指定VIP书籍ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmSQL t_frm = new FrmSQL();
            DialogResult dlr = t_frm.ShowDialog();
            if (dlr == DialogResult.OK)
            {
                VIPBookCollectManager t_vip_mgr = new VIPBookCollectManager();
                t_vip_mgr.BookSql = t_frm.BookSQL;
                t_vip_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
                Thread thd = new Thread(t_vip_mgr.StartVipBookList);
                thd.Start();
            }
        }

        private void vIP书籍普通ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
                 VIPBookCollectManager t_vip_mgr = new VIPBookCollectManager();
            t_vip_mgr.SendMsg += new SendMsgEventHandler(DisplayMsg);
            Thread thd = new Thread(t_vip_mgr.CollectVipBookNormalChapter);
            thd.Start();
        }

        private void 测试ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NetSiteCatchManager.DownLoadFile("http://file-uu-cp.kakapan.com/31/31647/845577.dec", "D:\\work\\my_project\\source_control\\HT\\2.dec");
            string t_chapter= NetSiteCatchManager.ReadUrl("http://file-uu-cp.kakapan.com/31/31647/845577.dec", Encoding.GetEncoding("ANSI"));

          
        }

    }
}
